<template>
	<view class="root">
		<view class="search-container">
			<!-- 搜索商品 -->
			<goods-search class="search-box" bgcolor="#c00000" :radius="18" @searchClick="toSearch"></goods-search>
		</view>
		<!-- 轮播图 -->
		<swiper class="uni-swiper" :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000">
			<swiper-item v-for="(item, i) in swiperList" :key="i">
				<navigator class="swiper-item" :url="'/subpkg/detail/detail?goods_id='+item.goods_id">
					<image :src="item.image_src"></image>
				</navigator>
			</swiper-item>
		</swiper>
		<!-- 分类 -->
		<view class="goods-cate">
			<view class="cate-item" v-for="(item, i) in navList" :key="i" @click="navClickHandler(item)">
				<image :src="item.image_src"></image>
				<text>{{name}}</text>
			</view>
		</view>
		<!-- 楼层数据 -->
		<view class="floor-item"  v-for="(item, i) in floorData" :key="i">
			<image :src="item.floor_title.image_src" class="floor-item-title"></image>
			<view class="floor-item-pro">
				<navigator class="pro-left" :url="item.product_list[0].url">
					<image 
					:src="item.product_list[0].image_src" 
					:style="{width:item.product_list[0].image_width+'rpx'}" 
					mode="widthFix"></image>
				</navigator>
				<view class="pro-right">
					<navigator 
						v-for="(pro, pi) in item.product_list" 
						:key="pi" 
						v-if="pi !== 0" 
						:url="pro.url"
						class="pro-image">
						<image :src="pro.image_src" :style="{width:pro.image_width+'rpx'}" mode="widthFix"></image>
					</navigator>
				</view>
			</view>
		</view>
	</view>
	
</template>

<script>
	import badgeMix from '@/mixins/tabbar-badge.js'
	export default {
		mixins:[badgeMix],
		data() {
			return {
				swiperList: [], //轮播图数据
				navList: [], //导航区域
				floorData: [] //楼层数据
			};
		},
		onLoad() {
			//调用获取轮播图方法
			this.getSwiperList()
			//获取导航数据
			this.getNavList()
			//获取路层数据
			this.getFloorData()
		},
		methods: {
			async getSwiperList() {
				const {
					data
				} = await uni.$http.get("/api/public/v1/home/swiperdata");
				if (data.meta.status !== 200) {
					return uni.$showMsg();
				}
				this.swiperList = data.message;
			},
			async getNavList() {
				const {
					data
				} = await uni.$http.get("/api/public/v1/home/catitems");
				if (data.meta.status !== 200) {
					return uni.$showMsg();
				}
				this.navList = data.message;
			},
			navClickHandler(item) {
				if (item.open_type && item.open_type === "switchTab") {
					uni.switchTab({
						url: "/pages/cate/cate"
					})
				}
			},
			async getFloorData() {
				const {
					data
				} = await uni.$http.get("/api/public/v1/home/floordata");
				if (data.meta.status !== 200) {
					return uni.$showMsg();
				}
				data.message.forEach(floor => {
					floor.product_list.forEach(prod => {
						prod.url = "/subpkg/goods_list/goods_list?"+prod.navigator_url.split('?')[1]
					})
				})
				this.floorData = data.message;
			},
			toSearch(){
				uni.navigateTo({
					url:"/subpkg/search/search"
				})
			}
		}
	}
</script>

<style lang="scss">
	.root {
		margin: 0;
		padding: 0;

		//轮播图
		.uni-swiper {
			height: 330rpx;
			.swiper-item,
			image {
				width: 100%;
				height: 100%;
			}
		}
		//图标
		.goods-cate {
			display: flex;
			justify-content: space-around;
			margin: 15rpx 0;

			.cate-item {
				width: 128rpx;
				height: 140rpx;
				
				image {
					width: 100%;
					height: 100%;
				}
			}
		}
		//楼层
		.floor-item{
			margin-top: 25rpx;
			.floor-item-title{
				width: 100%;
				height: 60rpx;
			}
			.floor-item-pro{
				display: flex;
				justify-content: space-between;
				width: 100%;
				.pro-right{
					display: flex;
					flex-wrap: wrap;
					justify-content: space-around;
				}
			}
		}
	  .search-container {
			position: sticky;
			top: 0;
			z-index: 999;
		}
	}
</style>